import os
import sys
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.common.action_chains import ActionChains
class LikeShopTest():
    def __init__(self) -> None:

        driver_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(sys.executable)))),
                               'geckodriver.exe')
        service = FirefoxService(executable_path=driver_path)
        options = webdriver.FirefoxOptions()
        options.binary_location = r"C:\Users\jx\AppData\Local\Mozilla Firefox\firefox.exe"
        self.browser = webdriver.Firefox(service=service, options=options)


    def test_login(self):
        self.browser.get('http://likeshop.abc/admin/account/login.html')
        sleep(2)
        #定位用户名输入框输入用户名
        self.browser.find_element(By.NAME,'account').send_keys('admin')
        #定位密码输入框密码
        self.browser.find_element(By.NAME,'password').send_keys('123456')
        #定位登录按钮并点击登录
        self.browser.find_element(By.ID,'login').click()
        sleep(2)
        
    def test_product_list(self):
        #定位商品标签并点击商品
        self.browser.find_element(By.XPATH,'//*[@id="LAY-system-side-menu"]/li[1]/a/cite').click()
        sleep(2)
        #定位子框架
        iframe = self.browser.find_element(By.XPATH,'//*[@id="LAY_app_body"]/div[2]/iframe')
        #切换到子框架
        self.browser.switch_to.frame(iframe)
        #定位商品名称并输入文具
        self.browser.find_element(By.NAME,'keyword').send_keys('文具')
        #定位查询按钮并点击查询
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[1]/div/div[5]/div[3]/button[1]').click()
        #接受返回的商品列表
        #定位商品列表
        product_list = self.browser.find_elements(By.XPATH,"/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr")
        if len(product_list) == 0:
            print('没有查询到商品')
        else:
            #遍历商品列表
            for product in product_list:
                #定位商品名称
                product_name = product.find_element(By.XPATH,'./td[2]/div').text
                if '文具' not in product_name:
                    print("有BUG")
        test_passed = True
        is_not_bug = test_passed
        if is_not_bug:
            print("没有BUG")
        #退出子框架
        self.browser.switch_to.default_content()
        
    def test_logout(self):
        #定位用户信息
        dt = self.browser.find_element(By.XPATH,'/html/body/div/div[1]/div[1]/ul[2]/li[3]/a/cite')
        #鼠标悬停在用户信息上
        ActionChains(self.browser).move_to_element(dt).perform()
        sleep(2)
        #定位退出按钮并点击退出
        self.browser.find_element(By.XPATH,'/html/body/div/div[1]/div[1]/ul[2]/li[3]/dl/dd[2]/a').click()
        sleep(2)

    def test_product_add(self):
        #定位商品标签并点击商品
        self.browser.find_element(By.XPATH,'//*[@id="LAY-system-side-menu"]/li[1]/a/cite').click()
        sleep(2)
        #定位子框架
        iframe = self.browser.find_element(By.XPATH,'//*[@id="LAY_app_body"]/div[2]/iframe')
        #切换到子框架
        self.browser.switch_to.frame(iframe)
        #定位发布商品按钮并点击发布商品
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[2]/div[1]/button[1]').click()
        sleep(2)
        #定位商品发布子框架
        #切换到商品发布子框架
        iframe_add = self.browser.find_element(By.XPATH,'/html/body/div[3]/div[2]/iframe')
        self.browser.switch_to.frame(iframe_add)
        #定位商品名称输入框输入商品名称
        self.browser.find_element(By.NAME,'name').send_keys('测试商品')
        sleep(2)
        #定位第一个类别并选择类别
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/div/input').click()
        sleep(1) 
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/dl/dd[2]').click()
        sleep(1) 
        #定位第二个类别并选择类别
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/div/input').click()
        sleep(1) 
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/dl/dd[2]').click()
        sleep(1)
        #定位第三个类别并选择类别
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/div/input').click()
        sleep(1) 
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/dl/dd[2]').click()
        sleep(1)
        
        #定位商品主图的添加图片组件并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[5]/div/div[2]/a').click()
        #上传商品主图
        self.test_upload_img('D:\\project\\aaa.png','/html/body/div[6]/div[2]/iframe')
        #切换到商品发布子框架
        self.browser.switch_to.frame(iframe_add)
        #定位商品轮播图的添加图片组件并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[6]/div/div/a').click()
        #上传商品轮播图
        self.test_upload_img('D:\\project\\aaa.png','//*[@id="layui-layer-iframe3"]')
        self.browser.switch_to.frame(iframe_add)
        #切换到价格库存标签页
        self.browser.find_element(By.XPATH,'/html/body/div/ul/li[2]').click()
        sleep(3)
        # self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[4]/div/dl/dd[2]').click()
        # sleep(1)
        
        
        #定位商品主图的添加图片组件并点击
        #定位市场（元）输入框输入市场价（元）
        self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[2]/input').send_keys('1')
        sleep(1)
        #定位价格（元）输入框输入价格（元）
        self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[3]/input').send_keys('1')
        sleep(1)
        #定位成本价(元)输入框输入成本价(元)
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[4]/input').send_keys('1')
        sleep(1)
        #定位库存输入框输入库存
        self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[5]/input').send_keys('1000')
        sleep(1)
        #切换到销售设置标签页
        self.browser.find_element(By.XPATH,'/html/body/div/ul/li[4]').click()
        sleep(3)
        #选择配送方式的快递发货
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[4]/div[6]/div/div[1]/i').click()
        sleep(1)
        #返回商品子框果
        self.browser.switch_to.parent_frame()
        #点击保存
        self.browser.find_element(By.XPATH,'//*[@id="layui-layer1"]/div[3]/a[1]').click()
        sleep(2)

    def test_upload_img(self,img_path, xpath_path):

        sleep(4)
        #返回商品子框架
        self.browser.switch_to.parent_frame( )
    
        #定位图片上传子框架
        #切换到图片上传子框架
        upload_img_iframe_1 = self.browser.find_element(By.XPATH, xpath_path)
        self.browser.switch_to.frame(upload_img_iframe_1)

        #定位图片输入框输入图片路径
        self.browser.find_element(By.XPATH, '/html/body/div/div[2]/div[1]/div[1]/input').send_keys(img_path)
        sleep(4)
        #选择图片
        self.browser.find_element(By.XPATH, '//*[@id="lists"]/li[1]/div/img').click()
        sleep(2)
        #点击使用图片
        self.browser.find_element(By.XPATH,'//*[@id="used"]').click()
        sleep(2)
        #返国商品子框架
        self.browser.switch_to.parent_frame()


    def test_product_shangjia(self):
        #定位商品标签井点击商品
        self.browser.find_element(By.XPATH, '//*[@id="LAY-system-side-menu"]/li[1]/a/cite').click()
        sleep(2)
        #定位子框架
        iframe = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
        #切换到子框架
        self.browser.switch_to.frame(iframe)
        #切换到仓库中商品
        self.browser.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/ul/li[2]').click()
        sleep(2)
        #接收列表
        tr_list = self.browser.find_elements(By.XPATH,'/html/body/div[1]/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr')
        if len(tr_list) == 0:
            print('仓库中暂无商品')
            return
        #点击第一个商品的上架按钮
        tr_list[0].find_element(By.XPATH, './td/div/a[2]').click()
        sleep(1)
        #确认上架
        self.browser.find_element(By.CLASS_NAME,'layui-layer-btn0').click()
        sleep(2)
        
        
if __name__== '__main__':
    lk = LikeShopTest()
    lk.test_login()
    lk.test_product_list()
    # lk.test_product_add()
    lk.test_product_shangjia()
    lk.test_logout()
        